
//
//  MAWorkDetailCell.m
//  meiang_edu
//
//  Created by 陈义蒙 on 2018/9/30.
//  Copyright © 2018年 com.meiang.meiang_edu. All rights reserved.
//  上传作业

#import "MAWorkDetailCell.h"
#import "MAHomeWorkEditFinishVC.h"

@interface MAWorkDetailCell ()
@property (nonatomic, strong) UIImageView *imgView;
@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) UIImageView *upImg;
@property (nonatomic, strong) UILabel *upL;
@end

@implementation MAWorkDetailCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor redColor];
        [self setSelectionStyle:UITableViewCellSelectionStyleNone];
        [self ma_addSubviews];
    }
    return self;
}

#pragma mark - private
- (void)ma_addSubviews {
    
    self.contentL = [[UILabel alloc]init];
    [self addSubview:self.contentL];
    self.contentL.font = [UIFont systemFontOfSize:14];
    self.contentL.numberOfLines = 0;
    self.contentL.lineBreakMode = NSLineBreakByWordWrapping;
    [self.contentL mas_makeConstraints:^(MASConstraintMaker *make)
    {
        make.top.equalTo(self.contentView.mas_top).offset(12 * kScale);
        make.left.equalTo(self.contentView.mas_left).offset(12 * kScale);
        make.right.equalTo(self.contentView.mas_right).offset(-12 * kScale);
    }];
    
    [self addSubview:self.imgView];
    [self.imgView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.height.width.equalTo(@30);
        make.top.equalTo(self.contentL.mas_bottom);
        make.left.equalTo(@(SCREEN_WIDTH/4));
    }];
    
    [self addSubview:self.titleLabel];
    [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.imgView.mas_bottom).offset(12);
        make.left.equalTo(@(SCREEN_WIDTH/4));
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-10 * kScale);
    }];
    
    [self addSubview:self.upImg];
    [self.upImg mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.height.width.equalTo(@30);
        make.top.equalTo(self.imgView);
        make.left.equalTo(@(SCREEN_WIDTH*0.75));
    }];
    
    [self addSubview:self.upL];
    [self.upL mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.imgView.mas_bottom).offset(12);
        make.left.equalTo(@(SCREEN_WIDTH*0.75));
    }];
    
}

#pragma mark - getter
- (UIImageView *)imgView {
    if (!_imgView) {
        _imgView = [[UIImageView alloc] init];
        _imgView.image =[UIImage imageNamed:@"apply_logo"];
        _imgView.userInteractionEnabled = YES;
        
        UITapGestureRecognizer *ges = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(saveWork)];
        [_imgView addGestureRecognizer:ges];
    }
    return _imgView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = [UIColor colorWithHexString:@"#333333"];
        _titleLabel.font = [UIFont systemFontOfSize:10];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.text = @"未完成";
    }
    return _titleLabel;
}

- (UIImageView *)upImg {
    if (!_upImg) {
        _upImg = [[UIImageView alloc] init];
        _upImg.image =[UIImage imageNamed:@"uploadingbutton_logo"];
        
        _upImg.userInteractionEnabled = YES;
        UITapGestureRecognizer *ges = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(uploadWork)];
        [_upImg addGestureRecognizer:ges];
    }
    return _upImg;
}

- (UILabel *)upL {
    if (!_upL) {
        _upL = [[UILabel alloc] init];
        _upL.textColor = [UIColor colorWithHexString:@"#333333"];
        _upL.font = [UIFont systemFontOfSize:10];
        _upL.textAlignment = NSTextAlignmentCenter;
        _upL.text = @"上传作业";
    }
    return _upL;
}

-(void)setDicInfo:(NSDictionary *)dicInfo{
    _dicInfo = dicInfo;
    
    self.contentL.text = dicInfo[@"zy_content"];
    int finished = [dicInfo[@"finished"] intValue];
    if (finished == 1) {
        self.imgView.image = [UIImage imageNamed:@"finishedbutton_logo"];
        self.titleLabel.text = @"已完成";
    }else{
        self.imgView.image = [UIImage imageNamed:@"apply_logo"];
        self.titleLabel.text = @"未完成";
    }
}

-(void)saveWork{
    if ([self.dicInfo[@"finished"] intValue] ==1) {
        return ;
    }else{
        
        NSDictionary *param = @{
                                @"id": self.dicInfo[@"id"],
                                @"ch_id": self.reciever_key, //学生id
                                @"msg_id": self.dicInfo[@"msg_id"],
                                @"finished": @"1",
                                };
        
        [MBManager showLoading];
        
        NSError *error;
        
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:param options:0 error:&error];
        
        NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        
        AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
        
        NSMutableURLRequest *req = [[AFJSONRequestSerializer serializer] requestWithMethod:@"POST" URLString:SaveWork_url parameters:nil error:nil];
        
        [req setValue:@"application/text" forHTTPHeaderField:@"Content-Type"];
        
        [req setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        
        [req setHTTPBody:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
        
        [[manager dataTaskWithRequest:req completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
            [MBManager hideAlert];
            if (!error) {
                DLog(@"Reply JSON: %@", responseObject);
                NSInteger msgcode = [responseObject[@"msgcode"] integerValue];
                NSString *message = responseObject[@"message"];
                if (msgcode == 1) {
                    [MBManager showBriefAlert:message];
                } else {
                    [MBManager showBriefAlert:message];
                }
            } else {
                DLog(@"Error: %@, %@, %@", error, response, responseObject);
                [MBManager showBriefAlert:@"网络错误"];
            }
        }] resume];
       
    }
}

-(void)uploadWork{
    
    MAHomeWorkEditFinishVC *vc = [[MAHomeWorkEditFinishVC alloc]init];
    vc.or_id = self.sender_key;
    vc.keyid = self.keyid;
    [self.viewController.navigationController pushViewController:vc animated:YES];
}

-(void)setReciever_key:(NSString *)reciever_key{
    _reciever_key = reciever_key;
    
}
@end
